import GameTool from "../common/tool/GameTool";
import RandTool from "../common/tool/RandTool";
import ContextComponent from "../common/tool/context/ContextComponent";
import GameSnake from "./GameSnake";
import BodyBase from "./body/BodyBase";
import LocalCommon from "./config/LocalCommon";
import MapBase from "./map/MapBase";
const { ccclass, property } = cc._decorator;
@ccclass
export default class GameMap extends ContextComponent {
    /**
     * 奖励
     */
    @property(cc.Prefab)
    public PrefabAwardCube: cc.Prefab = null;

    @property(cc.Prefab)
    public PrefabHardCube: cc.Prefab = null;

    @property(cc.Prefab)
    public PrefabFirstCube: cc.Prefab = null;

    @property(cc.Prefab)
    public PrefabCubeGroup: Array<cc.Prefab> = [];
    @property(cc.Prefab)
    public PrefabLongBarGroup: Array<cc.Prefab> = [];
    @property(cc.Prefab)
    public PrefabShortBarGroup: Array<cc.Prefab> = [];

    protected mAddGroupIndex: number = 0;
    protected mAddHardGroup: number = 4;

    protected mGroupArray: Array<cc.Node> = [];

    protected mGameSnake: GameSnake = null;
    protected mOffest: number = 0;
    protected mIsFirst: boolean = true;
    protected mIsPause: boolean = false;
    public set IsPause(is: boolean) {
        this.mIsPause = is;
    }
    public get IsPause(): boolean {
        return this.mIsPause;
    }

    public load(): void {
        this.mGameSnake = this.getContext(GameSnake);
    }

    getRandomOne(array: Array<any>) {
        if (array.length == 0) {
            return null;
        }
        return array[RandTool.GetArrayRandIndex(array)];
    }
    public createNextGroup(): void {
        let group: cc.Node = null;
        if (this.mAddGroupIndex < 16) {
            if (this.mAddGroupIndex === 6 || this.mAddGroupIndex == 12)
                group = cc.instantiate(this.PrefabFirstCube);
            else
                group = cc.instantiate(this.PrefabAwardCube);
        }
        else {
            this.mAddHardGroup--;
            if (this.mAddHardGroup <= 0) {
                this.mAddHardGroup = 10;
                group = cc.instantiate(this.PrefabHardCube);
            }
            else {
                let addNumber = this.mAddHardGroup % 2;
                if (addNumber === 0) {
                    let randNumber = RandTool.RandIncludeMin(0, 4);
                    if (randNumber <= 0) {
                        group = cc.instantiate(this.PrefabAwardCube);
                    }
                    else {

                        if (this.mGameSnake.SnakeLength > 20)
                            group = cc.instantiate(this.getRandomOne(this.PrefabCubeGroup));
                        else {

                            let randNumber = RandTool.RandIncludeAll(0, 3);
                            if (GameTool.IsSafeArrayIndex(randNumber, this.PrefabCubeGroup)) {
                                group = cc.instantiate(this.PrefabCubeGroup[randNumber]);
                            }
                            else {
                                group = cc.instantiate(this.PrefabCubeGroup[0]);
                            }

                        }
                    }
                }
                else {
                    let isLong = RandTool.RandPlusOrMinusAsBool();
                    if (isLong)
                        group = cc.instantiate(this.getRandomOne(this.PrefabLongBarGroup));
                    else
                        group = cc.instantiate(this.getRandomOne(this.PrefabShortBarGroup));
                }
            }
        }
        if (group) {
            group.position = cc.v3(0, this.mOffest);
            this.node.addChild(group);
            this.mOffest += (group.height * LocalCommon.MAP_ADD_OFFEST);

            let map = group.getComponent(MapBase);
            map.init(this.mContextPool);
            this.mAddGroupIndex++;

            this.mGroupArray.push(group);
        }
    }

    public onUpdate(dt: number): void {
        
        if (this.mGameSnake.SnakeHead) {
            if (this.mGameSnake.SnakeHead.y >= this.mOffest - cc.winSize.height * 1.5) {
                this.createNextGroup();
            }
        }
        this.clearOutSideGroup();
    }


    public clearOutSideGroup(): void {
        if (!this.mGameSnake.SnakeHead)
            return;
        for (let i = this.mGroupArray.length - 1; i >= 0; i--) {
            if (this.mGroupArray[i].y + cc.winSize.height <= this.mGameSnake.SnakeHead.y) {
                let group = this.mGroupArray[i];
                this.mGroupArray.splice(i, 1);
                group.destroy();
            }
        }
    }
}
